package com.tanhua.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice  //代表是一个全局异常处理器
public class GlobalExceptionAdvice {

    @ExceptionHandler(Exception.class) //所有异常都捕捉--调用
    public ResponseEntity handlerException(Exception ex) {
        System.out.println("错误是：" + ex.getMessage()); //给程序提示


        String msg = "后台生了错误，请稍后尝试";

        //控制自己的自定义异常
        if (ex instanceof BusinessException) {
            msg = ex.getMessage();
        }

        return ResponseEntity.status(500).body(msg);
    }

    //
}